/*
 * Copyright (c) 2021-2021. Gardel
 *
 * This file is part of Source2Pdf (https://gitee.com/gardel/source2pdf).
 *
 * Source2Pdf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Source2Pdf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Source2Pdf.  If not, see <http://www.gnu.org/licenses/>.
 */

package cn.sunxinao.source2pdf.view;

import cn.sunxinao.source2pdf.model.FileNode;
import cn.sunxinao.source2pdf.utils.TaskUtils;
import io.github.palexdev.materialfx.controls.MFXCheckTreeItem;
import io.github.palexdev.materialfx.controls.cell.MFXCheckTreeCell;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

public class FileExplorerTreeCell extends MFXCheckTreeCell<FileNode> {
    public FileExplorerTreeCell(MFXCheckTreeItem<FileNode> item) {
        super(item, 40);
    }

    @Override
    protected void render(FileNode data) {
        getChildren().add(createFileBox(data));
    }

    private HBox createFileBox(FileNode node) {
        ImageView imageView = new ImageView();
        TaskUtils.submitTask(() -> {
            Image fileIcon = node.getFileIcon();
            Platform.runLater(() -> imageView.setImage(fileIcon));
        });
        HBox hBox = new HBox(10, imageView, new Text(node.getDisplayName()));
        hBox.setAlignment(Pos.CENTER_LEFT);
        return hBox;
    }
}
